var openid = getQueryString("openid");

function writeInfo(){
  $.ajax({
    url: API_URL+'agent/getbyopenid?openid='+ openid,
	type: 'GET',
	dataType: "JSON",
	data:'',
	async: false,
	success: function (obj){
		if (obj.status.code == 0) {
			var myData = obj.data[0];
			if(myData.icon == null || myData.icon == ''){
				myData.icon = '../img/default.png';
			}
			person_head = $('.person_head').css({
				'background': 'url('+myData.icon+') no-repeat',
				'background-size': '100% 100%'
			});

			$('#id').val(myData.id);
			$('#name').val(myData.name);
			$('#gender').find("option[value='"+myData.gender+"']").attr("selected",true);
			var ageStr = myData.age;
			if(ageStr==0){
				ageStr="";
			}
			$('#age').val(ageStr);
			$('#birthday').val(myData.birthday);
			$('#native_place').val(myData.native_place);
			$('#political_status').val(myData.political_status);
			$('#education').find("option[value='"+myData.education+"']").attr("selected",true);
			$('#school').val(myData.school);
			$('#professional').val(myData.professional); 
		}   
	},
    error: function (data){
      console.log("服务器异常!");
    }
  });
}

function rewriteInfo(){
	
    $.ajax({
        url: API_URL+'agent/saveorupdate',
        type: 'POST',
        dataType: "JSON",
        data:$('#perInfo').serialize(),
        async: false,
        success: function (obj){
            if (obj.status.code == 0) {
                window.location = 'person_mainpage.html?openid='+openid;
            }else{
                alert('服务错误');
            }
        },
        error: function (data){
            console.log("服务器异常!");
        }
    });
}

$('#save').on('click',function(){
    rewriteInfo();
})